#coding=utf-8
import sys
import pygame
import os
from pygame.sprite import Group
import time
import threading
#添加环境变量，类，模块路径
path = os.path.dirname(__file__)
sys.path.append(path)
from settings import *
from ship import *
import game_function as gf
import bullet as blt
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard

#创建线程锁
lock = threading.Lock()


def screen_Refresh(setting, screen, stats, ship, aliens, bullets, sb,
                   play_button):
    '''屏幕刷新频率'''
    #创建时钟变量
    end_time_screen, stat_time_screen, time_save_screen = 0, 0, 0
    while True:
        #设置间隔
        time_interval_screen = round(end_time_screen - stat_time_screen, 4)
        time_save_screen = +time_interval_screen
        if time_save_screen // setting.screen_Refresh_rate < 1:
            time.sleep(setting.screen_Refresh_rate - time_save_screen)
            time_save_screen = 0
        elif time_save_screen // setting.screen_Refresh_rate >= 1:
            time_save_screen -= setting.screen_Refresh_rate
        #更新屏幕

        stat_time_screen = time.clock()
        if not setting.stop:
            lock.acquire()
            gf.update_screen(setting, screen, stats, ship, aliens, bullets, sb,
                             play_button)
            lock.release()
        end_time_screen = time.clock()


def run_game():
    pygame.init()  #初始化背景设置
    set_begin = Setting()  #初始设置的类
    screen = pygame.display.set_mode(
        (set_begin.screen_width, set_begin.screen_height))  #添加窗口
    pygame.display.set_caption("Alien Invasion")
    #设置背景色
    bg_color = (set_begin.bg_color)
    #创建一个飞船
    ship = Ship(set_begin, screen, path)
    #创建一个储存子弹的编组
    bullets = Group()
    #创建一个外星人编组
    aliens = Group()
    #创建外星人群
    gf.create_fleet(set_begin, screen, aliens, ship, path)
    #创建一个储存游戏统计信息的实例
    stats = GameStats(set_begin)
    #创建一个开始按钮
    play_button = Button(set_begin, screen, "Play")
    #创建一个记分榜
    sb = Scoreboard(set_begin, screen, stats, path)

    #开一个多线程来刷新屏幕
    t = threading.Thread(target=screen_Refresh,
                         args=(set_begin, screen, stats, ship, aliens, bullets,
                               sb, play_button))
    t.start()

    #创建时钟变量
    end_time, stat_time, time_save = 0, 0, 0

    #开始游戏主循环
    while True:
        #每秒更新180次数据,每次0.0055秒
        time_interval_date = round(end_time - stat_time, 4)
        time_save += time_interval_date
        if time_save // set_begin.update_rate < 1:
            time.sleep(set_begin.update_rate - time_save)
            time_save = 0
        elif time_save // set_begin.update_rate >= 1:
            time_save -= set_begin.update_rate
        stat_time = time.clock()
        #监听鼠标键盘事件
        gf.check_events(set_begin, screen, stats, ship, aliens, bullets, sb,
                        play_button, path)
        #飞船移动
        lock.acquire() 
        if not set_begin.stop:  #游戏没有暂停才能继续
            if stats.game_active:
                ship.update()  #飞船数据更新
                gf.update_aliens(set_begin, stats, screen, ship, aliens,
                                 bullets, sb, path)  #外星人数据更新
                #每次循环重新绘制屏幕
                gf.update_bullet(set_begin, screen, stats, ship, aliens,
                                 bullets, sb, path)
            #gf.update_screen(set_begin,screen,stats,ship,aliens,bullets,sb,play_button)
        lock.release()
        end_time = time.clock()


run_game()
